import axios from 'axios'
import { Dialog } from 'vant';
import api from './api';
import store from '@/store'
import router from '@/router'
import Toast from "@/utils/toast";

axios.defaults.timeout = 5000; //响应时间
axios.defaults.baseURL = '';   //配置接口地址

Toast.allowMultiple();
var toast;

const unkonwServerError = '请求失败!请联系管理员'
const codeLoginSuccess = 1;
const codeLoginFailed = 2;

//👽👽👽👽👽👽👽👽👽👽👽服务端执行结果👽👽👽👽👽👽👽👽👽👽👽
const ACTION_SUCCESS = 1;
const ACTION_FAILED = 2;
//👽👽👽👽👽👽👽👽👽👽👽服务端执行结果👽👽👽👽👽👽👽👽👽👽👽


axios.interceptors.request.use((config) => {
    config.headers['token'] = window.localStorage.getItem("token");
    return config;
}, (error) => {
    console.log('错误的传参')
    return Promise.reject(error);
});

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((response) => {

    // if (response.status != 200) {
    //     Toast.fail(unkonwServerError);
    //     return Promise.reject(unkonwServerError);
    // }
    const res = response.data;
    if (res.code == 401) {
        Dialog.confirm({
	    title: "友情提示",
            message: "未登录，重新登录？"
        })
            .then(() => {
                router.push({ path: "/login", query: { redirect: router.currentRoute.fullPath } });
                // Toast.fail("请重新登陆");
            })
            .catch(() => {
            });
        return Promise.reject("请重新登陆");
    }
    //登录
    if (response.config.url == api.login) {
        switch (res.code) {
            case codeLoginSuccess:
                console.log('出参----', response)
                console.log('登录成功')
                Toast.success(res.message, false);
                return Promise.resolve(res);
                break;
            case codeLoginFailed:
                Toast.toast(res.message);
                return Promise.reject(res.message);
                break;
            default:
                return res.data;
                break;
        }
    } else {
        switch (res.code) {
            case ACTION_SUCCESS:
                return res;
            case ACTION_FAILED:
                return res
                break;
            default:
                Toast.toast(unkonwServerError, false);
                return Promise.reject(unkonwServerError);
                break;
        }
    }
}, (error) => {
    console.log(" go to the error the error is " + error);
    Toast.toast("请求失败!请联系管理员", false);
    return Promise.reject(error);
});

function showLoading(loadingMessage, durationSecond = 0) {
    if (loadingMessage) {
        toast = Toast.loading({
            duration: durationSecond,
            mask: true,
            message: loadingMessage
        });
    }
}

function hideLoading() {
    if (toast) {
        toast.clear();
    }
}

//返回一个Promise(发送post请求)
export function fetchPost(url, params, loadingMsg) {
    showLoading(loadingMsg);
    console.log('入参---', params)
    // params.appCode = "WMS"
    // params.storageLogin = true

    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                hideLoading();
                console.log(response)
                if(response.code === 1){
                  resolve(response)
                }else{
                  reject(response.message)
                }
            }).catch((error) => {
                hideLoading();
                reject(error)
            })
    })
}
//返回一个Promise(发送get请求)
export function fetchGet(url, param, loadingMsg) {
    showLoading(loadingMsg, 1 * 200);
    return new Promise((resolve, reject) => {
        axios.get(url, { params: param })
            .then(response => {
                hideLoading();
                resolve(response)
            }, err => {
                hideLoading();
                reject(err)
            })
            .catch((error) => {
                hideLoading();
                reject(error)
            })
    })
}
//返回一个Promise(发送Delete请求)
export function fetchDelete(url, loadingMsg) {
    showLoading(loadingMsg);
    return new Promise((resolve, reject) => {
        axios.delete(url)
            .then(response => {
                hideLoading();
                resolve(response)
            }, err => {
                hideLoading();
                reject(err)
            })
            .catch((error) => {
                hideLoading();
                reject(error)
            })
    })
}
export default {
    fetchPost,
    fetchGet,
    fetchDelete
}
